package com.vids.utils;


import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class ImageUtil {

    public static void getImageFromViidAndSave(String url, String objectTypeId, String imageID, String type)
    {
        BufferedImage image = null;
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = dateFormat.format(currentDate);
        int hour = currentDate.getHours();
        try {

            URL imageUrl = new URL(url);
            image = ImageIO.read(imageUrl);

            File f = new File("/mount/data1/viid_picture/"+today+"/"+hour+"/"+type+"/"+objectTypeId);
            if(!f.exists()){
                f.mkdirs();
            }
            String fileName = imageID + ".jpg";
            File outputfile = new File(f,fileName);
            ImageIO.write(image, "jpg", outputfile);

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            image = null;
        }
    }

    public static void convertBase64ToImageAndSave(String base64Code,String objectTypeId,String imageID,String type)
    {
        BufferedImage image = null;
        byte[] imageByte = null;
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = dateFormat.format(currentDate);
        int hour = currentDate.getHours();

        try {
            imageByte = DatatypeConverter.parseBase64Binary(base64Code);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
            image = ImageIO.read(new ByteArrayInputStream(imageByte));
            bis.close();

            File f = new File("/mount/data1/picture/"+today+"/"+hour+"/"+type+"/"+objectTypeId);
            if(!f.exists()){
                f.mkdirs();
            }
            String fileName = imageID + ".jpg";
            File outputfile = new File(f,fileName);
            ImageIO.write(image, "jpg", outputfile);

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            imageByte = null;
            image = null;
        }
    }

    public static String computeMD5(String full_filename)
    {
        byte[] imageBytes;
        File file = new File(full_filename); // 替换为你的图片文件路径
        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(file);
            imageBytes = new byte[(int) file.length()];
            fis.read(imageBytes);
            System.out.println("file length:"+file.length());

            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(imageBytes);

            byte[] digest = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : digest)
            {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not find MD5 algorithm", e);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }

    public static String computeMD5(File file)
    {
        byte[] imageBytes;
        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(file);
            imageBytes = new byte[(int) file.length()];
            fis.read(imageBytes);

            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(imageBytes);

            byte[] digest = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : digest)
            {
                sb.append(String.format("%02x", b));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("Could not find MD5 algorithm", e);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "";
    }


    /**
     *
     * @param firstImg
     * @param secondImg
     * @return
     */
    public static boolean isImageContentMD5Same(String firstImg, String secondImg)
    {
        String firstMD5= computeMD5(firstImg);
        String secondMD5= computeMD5(secondImg);

        System.out.println("String===> firstMD5:"+firstMD5);
        System.out.println("String===> secondMD5:"+secondMD5);
        return firstMD5.equals(secondMD5);
    }

    public static boolean isImageContentMD5Same(File firstImg, File secondImg)
    {
        String firstMD5= computeMD5(firstImg);
        String secondMD5= computeMD5(secondImg);

        System.out.println("File===> firstMD5:"+firstMD5);
        System.out.println("File===> secondMD5:"+secondMD5);
        return firstMD5.equals(secondMD5);
    }

}
